package dp.mobile.system;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import dp.mobile.glwrapper.GLTextureLibrary;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.util.Log;

public class Utilities {
	private static final String TAG = "Utilities";
	
	public static void initializeApps(Context context){
		BASE_DIRECTORY = context.getFilesDir().toString();
		
		DATA_FOLDER			= BASE_DIRECTORY + "/data";
		FONT_FOLDER			= BASE_DIRECTORY + "/fonts";
		IMAGE_FOLDER		= BASE_DIRECTORY + "/images";
		AUDIO_FOLDER		= BASE_DIRECTORY + "/audio";
		LEVEL_FOLDER		= DATA_FOLDER + "/levels";
		CUTSCENE_FOLDER		= DATA_FOLDER + "/cutscenes";
		ENEMY_FOLDER		= IMAGE_FOLDER + "/enemies";
		BULLET_FOLDER		= IMAGE_FOLDER + "/bullets";
		INTERFACE_FOLDER	= IMAGE_FOLDER + "/interface";
		BUILDING_FOLDER		= IMAGE_FOLDER + "/buildings";
		TILESET_FOLDER		= IMAGE_FOLDER + "/tilesets";
		STORY_FOLDER		= IMAGE_FOLDER + "/story";
		TOWER_FOLDER		= BUILDING_FOLDER + "/towers";
		GEN_FOLDER			= BUILDING_FOLDER + "/generators";
		BGM_FOLDER			= AUDIO_FOLDER + "/bgm";
		SFX_FOLDER			= AUDIO_FOLDER + "/sfx";
		SAVE_FOLDER			= BASE_DIRECTORY + "/saves";
		HIGHSCORE_FOLDER	= BASE_DIRECTORY + "/highscores";
		
		//save file
		SAVE_FILE			= SAVE_FOLDER + "/game.sav";
		
		// resource
		GENDATA_FILE		= DATA_FOLDER + "/generators.xml";
	    BUFFDATA_FILE    	= DATA_FOLDER + "/buffs.xml";
		TINTDATA_FILE		= DATA_FOLDER + "/tints.xml";
		TOWERDATA_FILE		= DATA_FOLDER + "/towers.xml";
		CAMPAIGNLEVEL_FILE	= DATA_FOLDER + "/levellist.xml";

		//Images
		TILE_IMAGE			= INTERFACE_FOLDER + "/tile.png";
		TILECURSOR_IMAGE 	= INTERFACE_FOLDER + "/cursor.png";
		TOWERPANEL_IMAGE	= INTERFACE_FOLDER + "/panel.png";
		PANELARROW_IMAGE 	= INTERFACE_FOLDER + "/arrow.png";

		MAIN_FONT			= FONT_FOLDER + "/gautami.font";
		TITLE_FONT			= FONT_FOLDER + "/evil_dead.font";
		
		SharedPreferences pref = context.getSharedPreferences(PREFERENCES, 0);
		boolean assetCopied  = pref.getBoolean("asset_copied", false);
		
		if (!assetCopied) {
			copyAssetFileToInternalMemory(context.getAssets(), BASE_DIRECTORY);
			
			SharedPreferences.Editor editor = pref.edit();
			editor.putBoolean("asset_copied", true);
			editor.commit();
		}
		
		//Init all SFX
		SoundManager.instance(context).loadDefaultSFX();
		
		//Init save
		initSaveFile();
		
		//Init highscores dir
		initHighScoresDir();
		
		Log.d(TAG, "done copying");
	}
	
	public static float calculateDistance(float x1, float x2, float y1, float y2) {
		final float dx = x1 - x2;
		final float dy = y1 - y2;
		
		return (float)Math.sqrt(dx * dx + dy * dy);
	}
	
	private static void initSaveFile() {
		File saveDir = new File(SAVE_FOLDER);
		
		if (!saveDir.exists())
			saveDir.mkdirs();
		
		File saveFile = new File(SAVE_FILE);
		
		if (!saveFile.exists()) {
			try {
				saveFile.createNewFile();
				Log.i("this" , "FILE CREATED");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else {
			Log.i("this" , "FILE ALREADY CREATED");
		}
	}
	
	private static void initHighScoresDir(){
		File highScoreDir = new File(HIGHSCORE_FOLDER);
		
		if(!highScoreDir.exists())
			highScoreDir.mkdirs();
	}
	
	public static void copyAssetFileToInternalMemory(AssetManager asset, String dir) {
		try {
			String[] files = asset.list("");
			for (int i = 0; i < files.length; ++i) {
				if (!files[i].contains(".")) continue;
				copyFile(asset.open(files[i]), files[i], dir);
			}
			
			copyAllFile(asset, "data", dir);
			copyAllFile(asset, "data/levels", dir);
			copyAllFile(asset, "data/cutscenes", dir);
			copyAllFile(asset, "fonts", dir);
			copyAllFile(asset, "images/buildings/towers", dir);
			copyAllFile(asset, "images/buildings/generators", dir);
			copyAllFile(asset, "images/bullets", dir);
			copyAllFile(asset, "images/enemies", dir);
			copyAllFile(asset, "images/interface", dir);
			copyAllFile(asset, "images/story", dir);
			copyAllFile(asset, "images/tilesets", dir);
			copyAllFile(asset, "audio/bgm", dir);
			copyAllFile(asset, "audio/sfx", dir);
		} catch (IOException iEx) {
			Log.e(TAG, "" +iEx);
		}
	}
	
	private static void copyAllFile(AssetManager asset, String copyDir, String dir) throws IOException {
		String[] files = asset.list(copyDir);
		new File(dir + "/" + copyDir).mkdirs();
		for (int i = 0; i < files.length; ++i) {
			if (!files[i].contains(".")) continue;
			copyFile(asset.open(copyDir + "/" + files[i]), copyDir + "/" + files[i], dir);
		}
	}
	
	private static void copyFile(InputStream asset, String fileName, String dir) throws IOException {
		Log.d(TAG, "fileName = " + fileName + " Dir = " + dir);
		byte[] buffer = new byte[1024];
		BufferedInputStream bis = new BufferedInputStream(asset);
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dir + "/" + fileName));
		
		int read;
		while ((read = bis.read(buffer)) != -1) {
			bos.write(buffer, 0, read);
		}
		
		bis.close();
		bos.close();
		
		Log.d(TAG, "Done copying fileName = " + fileName);
	}
	
	public static void writeNewFile(InputStream asset, String fileName, String dir) throws IOException {
		Log.d(TAG, "fileName = " + fileName + " Dir = " + dir);
		byte[] buffer = new byte[1024];
		BufferedInputStream bis = new BufferedInputStream(asset);
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dir + "/" + fileName));
		
		int read;
		while ((read = bis.read(buffer)) != -1) {
			bos.write(buffer, 0, read);
		}
		
		bis.close();
		bos.close();
		
		Log.d(TAG, "Done copying fileName = " + fileName);
	}
	
	public static GLTextureLibrary sTextureLibrary;
	
	//system constant
	public static final String 	PREFERENCES 	= "EdenPref";
	public static final String	LAST_LEVEL_PREF	= "last_level";
	public static String 		BASE_DIRECTORY;
	public static final int		SCREEN_WIDTH 	= 800;
	public static final int		SCREEN_HEIGHT 	= 480;
	public static final int		SCROLL_FACTOR	= 1;
	
	//Game constants
	public static final int GAME_FPS 			= 30;
	public static final int GAME_BUTTON_WAIT 	= 200;
	public static final int SPLASH_TIME			= 3000;
	
	//Gameplay constants
	public static final int TILE_WIDTH			= 64;
	public static final int TILE_HEIGHT			= 64;
	public static final int TILESET_COLUMN		= 8;
	public static final int	ELECTRICITY_COLOR	= 0xFF0091C0;
	public static final int[] COMPONENT_COLOR	= new int[] {0xFF5E5EF5, 0xFFF55E5E, 0xFF5EF55E};
	public static final int	CURSOR_DEFAULT		= 0;
	public static final int	CURSOR_DISABLED		= 3;
	public static final int	FLOATING_TEXT_SPEED	= 32;
	public static final int	FLOATING_TEXT_DURAT	= 1500;
	public static final float MIN_DEPTH			= -4;
	public static final float MAX_DEPTH			= -1.01f;
	public static final float SPRITE_DEPTH		= 0.007f;
	public static final float BULLET_DEPTH		= 0.004f;
	public static final float CURSOR_DEPTH		= 0.008f;
	public static final float TEXT_DEPTH		= 0.009f;
	public static final float TOWER_DEPTH		= 0.006f;
	public static final float RANGE_DEPTH		= 0.003f;
	public static final float NODE_DEPTH		= 0.001f;
	public static final float MAP_DEPTH			= 0.000f;

	//Folder constants
	public static String DATA_FOLDER;
	public static String FONT_FOLDER;
	public static String IMAGE_FOLDER;
	public static String AUDIO_FOLDER;
	public static String LEVEL_FOLDER;
	public static String CUTSCENE_FOLDER;
	public static String ENEMY_FOLDER;
	public static String BULLET_FOLDER;
	public static String INTERFACE_FOLDER;
	public static String BUILDING_FOLDER;
	public static String TOWER_FOLDER;
	public static String GEN_FOLDER;
	public static String TILESET_FOLDER;
	public static String BGM_FOLDER;
	public static String SFX_FOLDER;
	public static String SAVE_FOLDER;
	public static String HIGHSCORE_FOLDER;
	public static String STORY_FOLDER;
	
	//save file
	public static String SAVE_FILE;
	
	// resource
	public static String GENDATA_FILE;
    public static String BUFFDATA_FILE;
	public static String TINTDATA_FILE;
	public static String TOWERDATA_FILE;
	public static String CAMPAIGNLEVEL_FILE;

	//Images
	public static String TILE_IMAGE;
	public static String TILECURSOR_IMAGE;
	public static String TOWERPANEL_IMAGE;
	public static String PANELARROW_IMAGE;
	
	//Resource name
	public static String COMPONENT_0_NAME = "biru";
	public static String COMPONENT_1_NAME = "merah";
	public static String COMPONENT_2_NAME = "ijo";

	//Fonts constants
	public static String MAIN_FONT;
	public static String TITLE_FONT;
	
	//Facebook thingy
	public static final String APP_ID = "115439535156240";
}
